/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.cadmiumjdbc;

import java.sql.Timestamp;
import java.util.Calendar;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Custom;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;
import fr.x9c.cadmium.primitives.cadmium.Cadmium;

/**
 * This class provides the primitives related to the <tt>java.sql.Timestamp</tt> class.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.1
 * @since 1.1
 */
@PrimitiveProvider
public final class Timestamps {

    /**
     * No instance of this class.
     */
    private Timestamps() {
    } // end empty constructor

    /**
     * Creates a timestamp instance corresponding to the current timestamp.
     * @param ctxt context
     * @param unit ignored
     * @return a new timestamp instance
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_timestamp_make(final CodeRunner ctxt,
                                                   final Value unit)
        throws Fail.Exception {
        try {
            return Cadmium.createObject(new Timestamp(Calendar.getInstance().getTimeInMillis()));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_timestamp_make(CodeRunner, Value)'

    /**
     * Creates a timestamp instance from an integer value.
     * @param ctxt context
     * @param v the number of milliseconds since the epoch
     * @return a new timestamp instance
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_timestamp_make_of_int64(final CodeRunner ctxt,
                                                            final Value v)
        throws Fail.Exception {
        try {
            return Cadmium.createObject(new Timestamp(v.asBlock().asInt64()));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_timestamp_make_of_int64(CodeRunner, Value)'

    /**
     * Creates a timestamp instance from a string value.
     * @param ctxt context
     * @param v the timestamp as a string
     * @return a new timestamp instance
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_timestamp_make_of_string(final CodeRunner ctxt,
                                                             final Value v)
        throws Fail.Exception {
        try {
            return Cadmium.createObject(Timestamp.valueOf(v.asBlock().asString()));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_timestamp_make_of_string(CodeRunner, Value)'

    /**
     * Compares two timestamps.
     * @param timestamp first timestamp
     * @param timestamp2 second timestamp
     * @return <tt>true</tt> if the first timestamp is strictly after the second one,
     *         <tt>false</tt> otherwise
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_timestamp_after(final CodeRunner ctxt,
                                                   final Value timestamp,
                                                   final Value timestamp2)
        throws Fail.Exception {
        try {
            final Timestamp ts = (Timestamp) timestamp.asBlock().asCustom();
            final Timestamp ts2 = (Timestamp) timestamp2.asBlock().asCustom();
            return ts.after(ts2) ? Value.TRUE : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_timestamp_after(CodeRunner, Value, Value)'

    /**
     * Compares two timestamps.
     * @param timestamp first timestamp
     * @param timestamp2 second timestamp
     * @return <tt>true</tt> if the first timestamp is strictly before the second one,
     *         <tt>false</tt> otherwise
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_timestamp_before(final CodeRunner ctxt,
                                                    final Value timestamp,
                                                    final Value timestamp2)
        throws Fail.Exception {
        try {
            final Timestamp ts = (Timestamp) timestamp.asBlock().asCustom();
            final Timestamp ts2 = (Timestamp) timestamp2.asBlock().asCustom();
            return ts.before(ts2) ? Value.TRUE : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_timestamp_before(CodeRunner, Value, Value)'

    /**
     * Compares two timestamps.
     * @param timestamp first timestamp
     * @param timestamp2 second timestamp
     * @return zero if passed timestamps are equal, a negative value if the first
     *         timestamp is before the second one, a positive value otherwise
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_timestamp_compare_to(final CodeRunner ctxt,
                                                        final Value timestamp,
                                                        final Value timestamp2)
        throws Fail.Exception {
        try {
            final Timestamp ts = (Timestamp) timestamp.asBlock().asCustom();
            final Timestamp ts2 = (Timestamp) timestamp2.asBlock().asCustom();
            return Value.createFromLong(ts.compareTo(ts2));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_timestamp_compare_to(CodeRunner, Value, Value)'

    /**
     * Returns the number of nanoseconds.
     * @param ctxt context
     * @param timestamp timestamp
     * @return the number of nanoseconds
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_timestamp_get_nanos(final CodeRunner ctxt,
                                                        final Value timestamp)
        throws Fail.Exception {
        try {
            final Timestamp ts = (Timestamp) timestamp.asBlock().asCustom();
            final Block res = Block.createCustom(Custom.INT_32_SIZE,
                                                 Custom.INT_32_OPS);
            res.setInt32(ts.getNanos());
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_timestamp_get_nanos(CodeRunner, Value)'

    /**
     * Returns the number of milliseconds since the epoch.
     * @param ctxt context
     * @param timestamp timestamp
     * @return the number of milliseconds since the epoch
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_timestamp_get_time(final CodeRunner ctxt,
                                                       final Value timestamp)
        throws Fail.Exception {
        try {
            final Timestamp ts = (Timestamp) timestamp.asBlock().asCustom();
            final Block res = Block.createCustom(Custom.INT_64_SIZE,
                                                 Custom.INT_64_OPS);
            res.setInt64(ts.getTime());
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_timestamp_get_time(CodeRunner, Value)'

    /**
     * Changes the number of nanoseconds.
     * @param ctxt context
     * @param timestamp timestamp
     * @param v the number of nanoseconds.
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_timestamp_set_nanos(final CodeRunner ctxt,
                                                        final Value timestamp,
                                                        final Value v)
        throws Fail.Exception {
        try {
            final Timestamp ts = (Timestamp) timestamp.asBlock().asCustom();
            ts.setNanos(v.asBlock().asInt32());
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_timestamp_set_nanos(CodeRunner, Value, Value)'

    /**
     * Changes the number of milliseconds since the epoch.
     * @param ctxt context
     * @param timestamp timestamp
     * @param v the number of milliseconds since the epoch
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_timestamp_set_time(final CodeRunner ctxt,
                                                       final Value timestamp,
                                                       final Value v)
        throws Fail.Exception {
        try {
            final Timestamp ts = (Timestamp) timestamp.asBlock().asCustom();
            ts.setTime(v.asBlock().asInt64());
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_timestamp_set_time(CodeRunner, Value, Value)'

    /**
     * Converts a timestamp into a string.
     * @param ctxt context
     * @param timestamp timestamp
     * @return a string corresponding to the passed timestamp
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_timestamp_to_string(final CodeRunner ctxt,
                                                        final Value timestamp)
        throws Fail.Exception {
        try {
            final Timestamp ts = (Timestamp) timestamp.asBlock().asCustom();
            final Block res = Block.createString(ts.toString());
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method ' cadmiumjdbc_timestamp_to_string(CodeRunner, Value)'

} // end class 'Timestamps'
